
package com.woniuxy.yogaapp.mapper;


import com.woniuxy.yogaapp.dto.UidDto;
import com.woniuxy.yogaapp.pojo.Chat;
import com.woniuxy.yogaapp.pojo.Coach;
import com.woniuxy.yogaapp.pojo.Student;
import org.apache.ibatis.annotations.*;
import com.woniuxy.yogaapp.dto.Check;

import java.util.List;

/**
 * 操作coach表
 */
public interface CoachMapper {

	/**
	 * 查询所有教练
	 *
	 * @return
	 */
	@Select("select * from coach where approve=2")
	List<Coach> findAllCoach();

	// 根据教练的uid查询对应场馆的名字
	@Select("select yname from clubcoach where cuid=#{cuid} and approve=2")
	List<String> findClubNameByUid(int cuid);

	/**
	 * 根据教练id查学员
	 *
	 * @param cid
	 * @return
	 */
	@Select("select * from coach where cid=#{cid}")
	@Results({ @Result(id = true, column = "cid", property = "cid"), @Result(column = "cname", property = "cname"),
			@Result(column = "cid", property = "students", many = @Many(select = "findStudentsByCid")) })
	Coach findByCid(int cid);

	@Select("select * from coachstudent cs,student s where cs.sid=s.sid and  cs.cid=#{cid}")
	List<Student> findStudentsByCid(Integer cid);

	/**
	 * 根据 uid 查询该用户 是不是新用户 返回不为空代表老用户
	 *
	 * @param coach
	 * @return
	 */
	@Select("select * from coach where uid=#{uid}")
	Coach findCoachByUid(Coach coach);

	/**
	 * 老用户 提交修改信息
	 *
	 * @param coach
	 * @return
	 */
	@Update("update coach set cname=#{cname},ctel=#{ctel},cqq=#{cqq},cage=#{cage},csex=#{csex},"
			+ "cimage=#{cimage},cpower=#{cpower},cnickname=#{cnickname},cmail=#{cmail},"
			+ "cgenre=#{cgenre},freetime=#{freetime},state=#{state},money=#{money}  where uid=#{uid}")
	int updateCoachInfo(Coach coach);

	/**
	 * 新用户 添加用户信息
	 *
	 * @param coach
	 * @return
	 */
	@Insert("insert into coach(cname,ctel,cqq,cage,csex,cimage,cpower,cnickname,uid,cmail,cgenre,freetime,state,money)"
			+ " values (#{cname},#{ctel},#{cqq},#{cage},#{csex}," + "#{cimage},#{cpower},#{cnickname},#{uid},#{cmail},"
			+ "#{cgenre},#{freetime},#{state},#{money})")
	int addCoachInfo(Coach coach);

	/**
	 * 瑜伽教练 添加资格认证信息待认证
	 *
	 * @param coach
	 * @return
	 */
	@Update("update coach  set cidcard=#{cidcard},caddress=#{caddress},`describe`=#{describe},caddrmes=#{caddrmes}   where uid=#{uid}")
	int approvePower(Coach coach);

	// 教练确定后更新订单状态,学完后
	@Update("UPDATE detailorder SET state=#{state} WHERE detailoid=#{detailoid}")
	public int updateOrder(Check c);

	/**
	 * 管理员查询所有未认证消息
	 *
	 * @return
	 */
	@Select("select * from coach where approve=0")
	List<Coach> findMes();

	// 认证教练成功，approve状态为2
	@Update("update coach set approve=2 where uid=#{uid}")
	int agreeCoach(int uid);

	// 认证教练驳回，approve状态为1
	@Update("update coach set approve=1 where uid=#{uid}")
	int disagreeCoach(int uid);

	// 根据cid找教练
	@Select("select * from coach where cid=#{cid} and approve=2")
	Coach findCoachbyCid(int cid);

	// 根据uid找教练
	@Select("select * from coach where uid=#{uid} and approve=2")
	Coach findCoachbyUid(int uid);

	// 为教练赋予权限
	@Insert("insert into userroles (uid,rid) values (#{uid},3)")
	int setCoach(int uid);

    @Update("UPDATE detailorder set transctionid=#{transctionid},otime=#{otime},state=#{state} WHERE detailoid=#{detailoid}")
    public int updateOrderCheck(Check c);
    //再次创建小订单
    @Insert("insert into detailorder (transctionid,otime,oid,state) values(#{transctionid},#{otime},#{oid},#{state})")
    public int reCreateOrder(Check check);
	@Insert("insert into coachstudent (cid,sid) values(#{cid},#{sid})")
    public int insertCidSid(Check c);
	@Select("select cid from coachstudent where cid=#{cid} and sid=#{sid}")
	public UidDto selectIsExist(Check c);
}
